package com.logic.landseaserver.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.Issue;
import com.logic.landseaserver.service.IIssueService;
import com.logic.landseaserver.service.utils.ResetQueryUtil;
import com.logic.landseaserver.ws.dto.IssueDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Path("/issue")
@Api(value="issue")
public class IssueRest {
	@Autowired
	private IIssueService issueServiceImpl;

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a new issue")
	public Response create(@ApiParam(value = "param", required = true) Issue issue) {
		CommonResultMap map = new CommonResultMap();
        try {
        	issue.setSerialNum(System.currentTimeMillis() + "");
        	int id = issueServiceImpl.insert(issue);
        	issue.setId(id);
            map.setData(issueServiceImpl.getDTO(id));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("create error.", e);
            map.setResult(e);
        } catch (Exception e) {
            logger.error("create error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}


	@DELETE
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "logic delete issue by set deleted")
    public Response logicDelete(@ApiParam(value = "param", required = true) @QueryParam("issueId") String ids) {
        CommonResultMap map = new CommonResultMap();
        try {
        	issueServiceImpl.delete(ids);
            map.setResult(CommonResult.SUCCESS,"delete issue");
        } catch (BusinessException e) {
            logger.error("logicDelete error.", e);
            map.setResult(e);
        }  catch (Exception e) {
            logger.error("logicDelete error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "edit issue ")
    public Response update(@ApiParam(value = "param", required = true) Issue issue) {
        CommonResultMap map = new CommonResultMap();
        try {
        	issueServiceImpl.update(issue);
            map.setResult(CommonResult.SUCCESS,"edit issue");
        } catch (BusinessException e) {
            logger.error("update error.", e);
            map.setResult(e);
        }  catch (Exception e) {
            logger.error("update error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @GET
    @Path("/detail/{issueId}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get issue by issue id")
    public Response retrieveDTO(
            @ApiParam(value = "param", required = true) @PathParam("issueId") Integer issueId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(issueServiceImpl.getDTO(issueId));
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("retrieveDTO error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


	@POST
	@Path("/detail/list")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list issues")
	public Response listDTO(@ApiParam(value = "param", required = true) QueryUtil queryUtil){
        CommonResultMap map = new CommonResultMap();
        List<IssueDTO> issueList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            ResetQueryUtil.reset(queryUtil);
            issueList = issueServiceImpl.queryDTO(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(issueList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);

        } catch (Exception e) {
            logger.error("listDTO error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

    @POST
    @Path("/check")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get issue by checkId")
    public Response retrieveByCheckId(
            @ApiParam(value = "param", required = true) QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        try {
        	queryUtil.getPagingTool().calculateStartIndex();
        	ResetQueryUtil.reset(queryUtil);
        	List<Issue> issueList = issueServiceImpl.queryByCheckId(queryUtil);
        	Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(issueList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("retrieveByCheckId error.", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

	@POST
	@Path("/copy/{issueId}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a new issue from template")
	public Response copy(@ApiParam(value = "param", required = true) Integer issueId) {
		CommonResultMap map = new CommonResultMap();
        try {
            map.setData(issueServiceImpl.copyFromTemplate(issueId));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("copy error.", e);
            map.setResult(e);
        }  catch (Exception e) {
            logger.error("copy error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}


}
